﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sample.Entity;
using Sample.Biz.BizAccess;
using Sample.Web.Base;
using Sample.Entity.Search;
using Project.Web.Base.Utility;

namespace Sample.Web.Models
{
	public class ModelSupplierIndex : BaseModel
	{
		private BizUTSupplier bizUTSupplier;
		/// <summary>
		/// 查询实体
		/// </summary>
		private SearchUTSupplier searchEntity;
		public SearchUTSupplier SearchEntity
		{
			get
			{
				if (searchEntity == null)
					return new SearchUTSupplier();
				return searchEntity;
			}
			set
			{
				searchEntity = value;
			}
		}

		/// <summary>
		/// 供应商列表
		/// </summary>
		public IList<UTSupplier> GridDataSources
		{
			get;
			set;
		}

		/// <summary>
		/// 支持多条件查询
		/// </summary>
		public void RetriveData()
		{
			ExpressionCondition<UTSupplier> condition = ExpressionCondition<UTSupplier>.GetInstance();
			if (string.IsNullOrEmpty(SearchEntity._CommonSearchCondition))
				condition.And(x => x.SupplierName, SearchEntity._SupplierName, ExpressionValueRelation.Like)
					.And(x => x.SupplyContent, SearchEntity._SupplyContent, ExpressionValueRelation.Like)
					.And(x => x.ContactName, SearchEntity._ContactName, ExpressionValueRelation.Like);
			else
				condition.Or(x => x.SupplierName, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.SupplyContent, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.ContactName, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Mobile, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Telephone, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Email, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Fax, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Address, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like);
			//bizUTSupplier.Test();
			GridDataSources = bizUTSupplier.PaginateListBy(SearchEntity._PageSize.Value, SearchEntity._PageIndex ?? 0, ref count, condition.ConditionExpression, SearchEntity._OrderName, SearchEntity.EnumOrderDirection);
			PaginateHelperObject = PaginateHelper.ConstructPaginate(SearchEntity._PageSize.Value, count, SearchEntity._PageIndex ?? 0, SearchEntity, "SearchEntity");

		}
	}


	public class ModelSupplierCreate : BaseModel
	{
		private BizUTSupplier bizUTSupplier;
		/// <summary>
		/// 供应商实体
		/// </summary>
		public UTSupplier UTSupplier
		{
			get;
			set;
		}


		/// <summary>
		/// 新增或者修改
		/// </summary>
		/// <param name="sampleCreateModel"></param>
		public void Save()
		{
			bizUTSupplier.SaveOrUpdate(UTSupplier);
		}
	}
}